<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-1.11.0.js"></script>
<script src="js/jquery.ztree.all-3.5.js"></script>
<script src="js/jquery.ztree.core-3.5.js"></script>
<link rel="stylesheet" href="css/zTreeStyle.css">
<script>
	var verifyCode;
	var jsonDropbox;
	
	function requestOauth() {
		$.ajax({
			url : "http://localhost:8080/rest/dropboxAPI/requestOauth",
			type : "GET",
			success : function(responseText) {
				var verifyUrl = responseText;			
				var win = window.open(verifyUrl, "windowname1", 'width=800, height=600');
				
				var pollTimer = window.setInterval(function() {
					try {
						if (win.document.URL.indexOf("uid") != -1) {
							window.clearInterval(pollTimer);
							verifyCode = win.document.URL.split("?")[1];	
							$("#tmpDiv").html("Authorization Dropbox successfully");
							win.close();			
							
							$.ajax({
								url : "http://localhost:8080/rest/dropboxAPI/requestAccess/" + verifyCode,
								type : "GET",
								dataType: 'JSON',
								global: false,
							    async: false,
								success : function(response){
									jsonDropbox = response;
								}
							});
						}
					} catch (e) {
					}
				}, 100);							
			}
		});
	}

	
	var OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?';
	var CLIENTID = '45339683173.apps.googleusercontent.com';

	var STATE = '';

	var REDIRECT = 'http://localhost:8080/login.html';
	var LOGOUT = 'http://accounts.google.com/Logout';
	var _url = OAUTHURL + 'client_id=' + CLIENTID + 
			'&redirect_uri=' + REDIRECT+'&response_type=token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.install%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.appdata';
	// https://www.dropbox.com/1/oauth2/authorize?client_id=<app key>&response_type=code&redirect_uri=<redirect URI>&state=<CSRF token>

	// https://accounts.google.com/o/oauth2/auth?client_id=45339683173.apps.googleusercontent.com&redirect_uri=http://localhost:8080/login.html&response_type=token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.install%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.appdata
	// Scope: \'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive.install https://www.googleapis.com/auth/drive.appdata\'
	// Scope: https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.install%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.appdata
	//var _url1= 'https://api.dropbox.com/1/account/info';
	
	function login() {
		var win = window.open(_url,"Login", 'width=700', 'height=500'); 

		var acToken;
		var tokenType;
		var uid;
		var pollTimer = window.setInterval(function() {
			try {

				console.log(win.document.URL);
				if (win.document.URL.indexOf(REDIRECT) != -1) {
					window.clearInterval(pollTimer);
					acToken = gup(win.document.URL, "access_token");
					document.getElementById("access_token").value = acToken;
					tokenType = gup(win.document.URL, "token_type");
					document.getElementById("token_type").value = tokenType;
					win.close();
					document.getElementById("form").submit();
				}
			} catch (e) {
			}
		}, 500);
	}
	function gup(url, name) {
		name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regexS = "[\\#&]" + name + "=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(url);
		if (results == null)
			return "";
		else
			return results[1];
	}
	
</script>
</head>
<body>

	<div id="tmpDiv">
		<h2>Let jQuery AJAX Change This Text</h2>
	</div>
	<div id="tree">
	</div>
	<button id="testbutton" onClick="requestOauth();">Go to Dropbox</button>	
	
	<form action="rest/googgledrive/login" method="post" id="form">
		<input type="hidden" id="access_token" name="access_token"/> <input type="hidden"
			id="token_type"  name="token_type"/> <input type="hidden" id="uid" name="uid" />
			<input type="button" value="Go to Google Drive" onclick="login()" />
	</form>
</body>
</html>
